package com.evil.common.application.enums.robot;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * 机器人首次触发时间
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum RobotFirstTriggerTypeEnum {

    /**
     * 机器人首次触发时间
     */
    //    DIY(0, "自定义"),
    ON_ADD(1, "新增数据时"),
    ON_MODIFY(2, "更新数据时"),
//    ON_DATE_CONTROL(3, "根据表单内时间字段"),
//    ON_ROBOT_FAIL(4, "机器人执行失败时"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<RobotFirstTriggerTypeEnum> findById(Integer id) {
        if (Objects.isNull(id)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static RobotFirstTriggerTypeEnum isExistById(Integer id) {
        return RobotFirstTriggerTypeEnum.findById(id).orElseThrow(RobotFirstTriggerTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "机器人首次触发参数错误");
    }
}
